﻿@model Coevery.Fields.Fields.DateField
@using Orchard.Utility.Extensions;
@using Coevery.Fields.Settings;
@{
    var settings = Model.PartFieldDefinition.Settings.GetModel<DateFieldSettings>();
    var required = settings.Required ? "required" : null;
}

<label class="control-label title @required" for="@Html.FieldIdFor(m => m.Value)">@Model.DisplayName</label>
<div class="control controls input-prepend errortips">
    <span class="add-on">
        <i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
    </span>
    <input class="dateForShort" type="text" 
           id="@Html.FieldIdFor(m => m.Value)" 
           name="@Html.FieldNameFor(m => m.Value)" 
           value="@Model.Value" 
           data-format="MM/dd/yyyy" 
        @if (required != null)
        {<text> required data-msg-required="Please fill this field!"</text> } 
        @if (!string.IsNullOrWhiteSpace(settings.HelpText)){<text>helper:text='@settings.HelpText'</text> }
        />
    @Html.ValidationMessageFor(m => m.Value)
</div>
<script type="text/javascript">
    function convertShortDate(dateStr) {
        if (dateStr) {
            var tempDate = new Date(dateStr);
            return (tempDate.getMonth() + 1).toString() + '/' + tempDate.getDate() + '/' + tempDate.getFullYear();
        } else {
            return "";
        }
    }

    $(function () {
        var defaultDate = $("#@Html.FieldIdFor(m => m.Value)");
        if (defaultDate.length != 0) {
            defaultDate.parent().datetimepicker({
                pickTime: false
            });
            defaultDate.val(convertShortDate(defaultDate.val()));
        }
    });
</script>
